﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public class EKLSData : BaseFunction
    { 
        public EKLSData()
            : base()
        {

        } 
        public EKLSData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        } 
        public DataSet BuildData(DataRow dwThis)
        {
            DataSet setReturn = new DataSet();
            DataSet dsResult = new DataSet();
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT LIEFNR,SUPPLIER,SUM(ORDERQTY) AS QUANTITY, "
                    + " SUM(VALUE) AS VALUE  FROM PURCHASE_REPORT  "
                    + " WHERE (ORDERDATE > :OPENORDERDATE1 OR :OPENORDERDATE1 IS NULL) "
                    + " AND (ORDERDATE < :OPENORDERDATE2 OR :OPENORDERDATE2 IS NULL)  GROUP BY LIEFNR,SUPPLIER "
                                ;

                orlReadCommand.Parameters.Add("OPENORDERDATE1", OracleType.NVarChar).Value = dwThis["OPENORDERDATE1"];
                orlReadCommand.Parameters.Add("OPENORDERDATE2", OracleType.NVarChar).Value = dwThis["OPENORDERDATE2"];

                dsResult = Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "PURCHASE_SUPP_SUMM";

                    byte[] b;
                    string value;
                    string ColumnName = "SUPPLIER";
                    foreach (DataRow dw in dsResult.Tables["PURCHASE_SUPP_SUMM"].Rows)
                    {
                        ColumnName = "SUPPLIER";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[ColumnName].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw[ColumnName] = value;
                    }
                }
                setReturn.Merge(dsResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
    }
}